<?php
/**
 *
 * User: 梁晓伟  lxw11109@gmail.com
 * Date: 2017-11-05
 * Time: 16:37
 */

namespace App\Http\Controllers\Api\V1\Manager\Index;


use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\UserHome;
use App\Models\UserStatistics;
use App\Models\VipLog;
use DateTime;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class IndexController extends Controller
{
    public function index(Request $request)
    {
        $dayTime = strtotime(date("Y-m-d"));
        $day = date("Y-m-d");
        $orderPrice = Order::whereNotIn('status', ['0', '1', '9'])->sum('oldAllPirce') + Order::whereNotIn('status', ['0', '1', '9'])->where('oldAllPirce', 0)->sum('allprice');
        $todayOrderPrice = (Order::whereNotIn('status', ['0', '1', '9'])->where('createtime', '>=', $dayTime)->where('oldAllPirce', 0)->sum('allprice')) + (Order::whereNotIn('status', ['0', '1', '9'])->where('createtime', '>=', $dayTime)->sum('oldAllPirce'));
        $order = [
            'orderPrice' => intval($orderPrice),
            'todayOrderPrice' => intval($todayOrderPrice),
            'vipPrice' => intval(VipLog::sum('vipPrice')),
            'todayVipPrice' => intval(VipLog::where('createTime', 'like', "%$day%")->sum('vipPrice')),
            'orderNum' => Order::count(),
            'afterOrderNum' => Order::where('is_shouhou', 1)->count(),
        ];
        $todayVipNum = UserStatistics::where('createDay', $day)->where('type', 3)->first();
        if ($todayVipNum) {
            $todayVipNum = $todayVipNum['num'];
        } else {
            $todayVipNum = 0;
        }
        $yesterdayVipNum = UserStatistics::where('createDay', date("Y-m-d", strtotime("-1 Day")))->where('type', 3)->first();
        if ($yesterdayVipNum) {
            $yesterdayVipNum = $yesterdayVipNum['num'];
        } else {
            $yesterdayVipNum = 0;
        }
        $todayUserNum = UserHome::where('createtime', '>=', $dayTime)->count();
        $yesterdayUserNum = UserHome::where('createtime', '<=', $dayTime)->where('createtime', '>=', strtotime(date("Y-m-d", strtotime("-1 Day"))))->count();
        $addUserRate = 0;
        $addVipRate = 0;
        if ($todayUserNum > $yesterdayUserNum) {
            $userNumDifference = $todayUserNum - $yesterdayUserNum;
            if ($yesterdayUserNum == 0) {
                $addUserRate = intval($userNumDifference * 100);
            } else {
                $addUserRate = intval(($userNumDifference / $yesterdayUserNum) * 100);
            }
        } else if ($todayUserNum < $yesterdayUserNum) {
            $userNumDifference = $yesterdayUserNum - $todayUserNum;
            $addUserRate = -(intval(($userNumDifference / $yesterdayUserNum) * 100));
        }
        if ($todayVipNum > $yesterdayVipNum) {
            $vipNumDifference = $todayVipNum - $yesterdayVipNum;
            if ($yesterdayVipNum == 0) {
                $addVipRate = intval($vipNumDifference * 100);
            } else {
                $addVipRate = intval(($vipNumDifference / $yesterdayVipNum) * 100);
            }
        } else if ($todayVipNum < $yesterdayVipNum) {
            $vipNumDifference = $yesterdayVipNum - $todayVipNum;
            $addVipRate = -(intval(($vipNumDifference / $yesterdayVipNum) * 100));
        }
        $user = [
            'userNum' => UserHome::count(),
            'addUserNum' => $todayUserNum,
            'addUserRate' => $addUserRate,
            'ptNum' => UserHome::where('isVip', 0)->count(),
            'addVipNum' => $todayVipNum,
            'addVipRate' => $addVipRate,
            'vipNum' => UserHome::where('isVip', 1)->count(),
        ];
        $data['order'] = $order;
        $data['user'] = $user;
        return $this->apiResponse($data);
    }

    public function indexPrice(Request $request)
    {
        $month = $request->month ?: $day = date("Y-m");
        $startDay = $month . "-01";
        $monthStartTime = strtotime($month . "-01 00:00:00");
        $monthEndTime = strtotime(date("Y-m-d H:i:s", strtotime("+1 month", ($monthStartTime)))) - 1;
        $orderList = Order::select('createtime', 'allprice', 'oldAllPirce')
            ->whereNotIn('status', ['0', '1', '9'])
            ->where('createtime', '>=', $monthStartTime)
            ->where('createtime', '<=', $monthEndTime)
            ->orderBy('createtime', 'asc')
            ->get()->toArray();
        $dataTime = [];
        $todayPrice = 0;
        $yesterdayPrice = 0;
        foreach ($orderList as $value) {
            $createDay = date("Y-m-d", $value['createtime']);
            if ($value['oldAllPirce'] != 0) {
                $price = $value['oldAllPirce'];
            } else {
                $price = $value['allprice'];
            }
            $price = intval($price * 100) / 100;
            if (isset($dataTime[$createDay])) {
                $dataTime[$createDay] += $price;
            } else {
                $dataTime[$createDay] = $price;
            }
            if ($createDay == date("Y-m-d")) {
                $todayPrice += $price;
            }
            if ($createDay == date("Y-m-d", strtotime("-1 Day"))) {
                $yesterdayPrice += $price;
            }
        }
        $orderPriceList = [];
        for ($i = 0; $i < 100; $i++) {
            if ($i == 0) {
                $day = $startDay;
            } else {
                $day = date("Y-m-d", strtotime("+$i Day", strtotime($startDay)));
            }
            if ($month != date("Y-m", strtotime($day))) {
                break;
            }
            $orderPriceList[$day] = 0;
            if (isset($dataTime[$day])) {
                $orderPriceList[$day] = round($dataTime[$day],2);
            }
        }
        $addPriceRate = 0;
        if ($todayPrice > $yesterdayPrice) {
            $priceNumDifference = $todayPrice - $yesterdayPrice;
            if ($yesterdayPrice == 0) {
                $addPriceRate = intval($priceNumDifference * 100);
            } else {
                $addPriceRate = intval(($priceNumDifference / $yesterdayPrice) * 100);
            }
        } else if ($todayPrice < $yesterdayPrice) {
            $priceNumDifference = $yesterdayPrice - $todayPrice;
            $addPriceRate = -(intval(($priceNumDifference / $yesterdayPrice) * 100));
        }
        $orderPriceTimeList = [];
        $i = 0;
        foreach ($orderPriceList as $key=>$value){
            $orderPriceTimeList[$i]['time'] = $key;
            $orderPriceTimeList[$i]['num'] = $value;
            $i++;
        }
        $data = [];
        $data['orderPriceList'] = $orderPriceTimeList;
        $data['todayPrice'] = $todayPrice;
        $data['yesterdayPrice'] = $yesterdayPrice;
        $data['addPriceRate'] = $addPriceRate;
        $data['month'] = $month;
        return $this->apiResponse($data);
    }
}